// pages/personal-information/index.js
const handler = require("../../utils/function.js");
const { $Message } = require('../../dist/base/index');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    sign:"请输入签名。。。",
    edit_open:true,
    user_type:wx.getStorageSync("user_type"),
    userData:[],
    user_info:wx.getStorageSync("user_info"),
    customePhone:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.updateUserInfo();
    this.getCustomePhone();
  },
  test_type: function () {
    const that = this;
    handler.requestData("userIsApply", { user_id: wx.getStorageSync("user_info").id }, function (res) {
      console.log(res);
      if (res.data!==1){
        wx.showModal({
          content: '如果你想成为导师，请先认证',
          success:function(e){
              if(e.confirm){
                handler.intoPageHandler("../apply-authority/index")
              }
          },
          fail:function(){

          }
        })
      }
      res.data == 2 && wx.showModal({
        content: '审核失败，请重新审核。。。',
      })
      that.setData({ is_certification: res.data });
    });
  },
  getCustomePhone:function(){
    var that = this;
    handler.requestData("customePhone", {  }, function (res) {
      that.setData({ customePhone:res.data})
    });
    
  },
  updateUserInfo: function () {
    var that = this;
    var id = wx.getStorageSync('user_info').id;
    if (id) {
      handler.requestData("getUserInfoById", { id: id }, function (res) {
        if (res.success == true) {
          wx.setStorageSync('user_info', res.data);
          that.setData({ user_info: res.data})
        } else {
          wx.showToast({
            title: res.message,
            icon: 'loading'
          })
        }
      });
    }
  },

  // 页面跳转
  handler_next_page(e) {
    handler.intoPageHandler(e.currentTarget.dataset.url);
  },
  handler_next_page_new:function(){
    const that=this;
    var user_type = wx.getStorageSync('user_info').user_type;
    const { is_certification, user_info}=this.data;
    if (user_info.user_type == 1){
      console.log(1);
      if (is_certification!==1){
        that.test_type();
        console.log(2);
      }else{
        console.log(3);
        handler.intoPageHandler('../teacher-appointment-record/index');
      }
    } else{
      console.log(4);
      handler.intoPageHandler('../user-appointment-record/index');
    }
  },
  handler_next_page_new_less:function(){
    var user_type = wx.getStorageSync('user_info').user_type;
    if (user_type == 1) {
      handler.intoPageHandler('../teacher-record/index');
    } else {
      handler.intoPageHandler('../lesson-record/index');
    }
  },
  into_page_next_new_avatar:function(){
    const that = this;
    var user_type = wx.getStorageSync('user_info').user_type;
    const { is_certification, user_info } = this.data;
    if (user_info.user_type == 1) {
      console.log(1);
      if (is_certification !== 1) {
        that.test_type();
        // console.log(2);
      } else {
        // console.log(3);
        handler.intoPageHandler('../teacher-setup-information/index');
      }
    } else {
      console.log(4);
      handler.intoPageHandler('../user-setup-information/index');
    }
    // var user_type = wx.getStorageSync('user_info').user_type;
    // if (user_type == 1) {
    //   handler.intoPageHandler('../teacher-setup-information/index');
    // } else {
    //   handler.intoPageHandler('../user-setup-information/index');
    // }
  },
  // 打电话
  callPhoneHandler:function(){
    var customePhone = this.data.customePhone;
    if (customePhone){
      handler.callPhoneHandler(customePhone)
    } else{
      wx.showToast({
        title: '没有客服',
        icon: 'loading'
      })
    }
   
  },
  //用户编辑
  edit_handler:function(){
    var {edit_open}=this.data;
    this.setData({ edit_open: !edit_open});
  },
  get_edit:function(e){
    this.setData({ sign: e.detail.value });
  },
  // 进入用户信息
  into_page_next:function(){
    // var type=wx.getStorageSync("user_type")
    // if (type==="teacher"){
    //   handler.intoPageHandler("../teacher-setup-information/index");
    // }
    // if (type === "user") {
    //   handler.intoPageHandler("../user-setup-information/index");
    // }
  },
  // 删除老师
  delete_teacher:function(){
    const that=this;
    wx.showModal({
      content: '是否退出导师功能',
      success:function(e){
        if(e.confirm){
          handler.requestData("switchUser", { phone: wx.getStorageSync("user_info").phone }, (res) => {
            // console.log(res);
            if (res.success === "fail") {
              handler.showToast({})
              that.updateUserInfo()
            } else {
              wx.setStorageSync("user-info", res.data)
              that.updateUserInfo()
            }
          })
        }
      }
    })
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.test_type();
    var that = this;
    var para = {
      "user_id": wx.getStorageSync('user_info').id,
      "user_type": wx.getStorageSync('user_info').user_type
    };
    handler.requestData("getUserInfo", para, function (res) {
      if (res.success == true) {
        that.setData({
          userData: res.data,
          sign: res.data.signature ? res.data.signature : '请输入签名'
        })
      } else {
        wx.showToast({
          title: res.message,
          icon: 'loading'
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  formSubmit: function (e) {
    var user_id = wx.getStorageSync('user_info').id;
    var formid = e.detail.formId;
    console.log(formid);
    if (user_id && formid) {
      var para = {
        form_id: formid,
        user_id: user_id
      };
      var that = this;
      handler.requestData("storageFormid", para, function (res) { console.log(res) });
    }
  }
})